package com.zenchn.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zenchn.annotation.OperLog;
import com.zenchn.entity.HttpDataResult;
import com.zenchn.enums.OperLogActionEnum;
import com.zenchn.model.TSysLoginAccessRecord;
import com.zenchn.model.TSysLoginAccessRecordExample;
import com.zenchn.service.LoginAccessRecordService;
import com.zenchn.utils.DateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

/**
 * 访问日志记录控制器
 *
 * @author dingxinfa
 * @date 2020-04-20 11:34
 */
@RestController
@RequestMapping(value = "loginAccessRecord")
@Api(value = "访问记录相关接口", tags = "日志查询-访问记录")
@RequiresPermissions("report:log:access:record")
public class LoginAccessRecordController {
    private final String MODULE = "日志查询>访问记录";

    @Resource
    private LoginAccessRecordService loginAccessRecordService;

    /**
     * 分页获取 访问记录列表
     * @param startTime
     * @param endTime
     * @param account
     * @param ip
     * @param page
     * @param pageSize
     * @param request
     * @param response
     * @return
     */
    @ApiOperation(value = "访问记录分页列表", notes = "访问记录分页列表")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Date", name = "startTime", value = "参数名称，开始时间"),
            @ApiImplicitParam(dataType = "Date", name = "endTime", value = "参数名称，结束时间"),
            @ApiImplicitParam(dataType = "String", name = "account", value = "参数名称，账号"),
            @ApiImplicitParam(dataType = "String", name = "ip", value = "参数名称，IP"),
            @ApiImplicitParam(dataType = "Integer", name = "page", value = "当前第page页"),
            @ApiImplicitParam(dataType = "Integer", name = "pageSize", value = "每页pageSize数量")
    })
    @OperLog(module = MODULE, action = OperLogActionEnum.QUERY, detail = "访问记录列表")
    @GetMapping(value = "/list")
    public ResponseEntity<HttpDataResult<PageInfo<TSysLoginAccessRecord>>> accessRecordList(
            @RequestParam(required = false, value = "startTime") @DateTimeFormat(pattern = "yyyy-MM-dd") Date startTime,
            @RequestParam(required = false, value = "endTime") @DateTimeFormat(pattern = "yyyy-MM-dd") Date endTime,
            @RequestParam(required = false, value = "account") String account,
            @RequestParam(required = false, value = "ip") String ip,
            @RequestParam(required = false, value = "page", defaultValue = "1") Integer page,
            @RequestParam(required = false, value = "pageSize", defaultValue = "10") Integer pageSize,
            HttpServletRequest request, HttpServletResponse response) {

        HttpDataResult<PageInfo<TSysLoginAccessRecord>> result = new HttpDataResult<>(request.getRequestURI());

        TSysLoginAccessRecordExample example = new TSysLoginAccessRecordExample();
        TSysLoginAccessRecordExample.Criteria criteria = example.createCriteria();
        if (null != startTime) {
            criteria.andAccessTimeGreaterThanOrEqualTo(LocalDateTime.ofInstant(DateUtils.getOneDayStartTime(startTime).toInstant(), ZoneId.systemDefault()));
        }
        if (null != endTime) {
            criteria.andAccessTimeLessThanOrEqualTo(LocalDateTime.ofInstant(DateUtils.getOneDayEndTime(endTime).toInstant(), ZoneId.systemDefault()));
        }
        if (!StringUtils.isEmpty(account)) {
            criteria.andLoginAccountLike(account.trim() + "%");
        }
        if (!StringUtils.isEmpty(ip)) {
            criteria.andAccessIpLike("%"+ip.trim() + "%");
        }
        example.setOrderByClause("access_time desc");

        PageHelper.startPage(page, pageSize);
        List<TSysLoginAccessRecord> recordList = loginAccessRecordService.selectByExample(example);
        PageInfo<TSysLoginAccessRecord> pageInfo = new PageInfo<>(recordList);

        result.setData(pageInfo);
        result.setMessage("获取访问记录列表成功");
        result.setStatus(HttpStatus.OK.value());


        return new ResponseEntity<>(result, HttpStatus.OK);
    }


}
